package com.kitty.web.client.controller;

import com.kitty.common.core.SchedulerManager;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.role.model.Role;
import com.kitty.web.client.AiInfo;
import com.kitty.web.client.ClientRoleManage;
import com.kitty.web.client.ClientSessionManage;
import com.kitty.web.client.service.AiService;
import com.kitty.web.common.AjaxResult;
import org.apache.mina.core.session.IoSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

@Controller
@RequestMapping("/system/ai")
public class AiController {
    @Autowired
    AiService aiService;
    @Autowired
    Dao dao;
    @PostMapping("/createAi")
    @ResponseBody
    public AjaxResult createAi(AiInfo aiInfo){
        aiService.createAi(aiInfo);
        return AjaxResult.success();
    }
    @PostMapping("/joinAi")
    @ResponseBody
    public AjaxResult joinAi(AiInfo aiInfo){
        aiService.joinAi(aiInfo);
        return AjaxResult.success();
    }

    @PostMapping("/loginAi")
    @ResponseBody
    public AjaxResult loginAi(AiInfo aiInfo){
        aiInfo.setLevel(Integer.parseInt(aiInfo.getLoginType()));
        SpringUtils.getBean(ClientRoleManage.class).loginAI(aiInfo);
        return AjaxResult.success();
    }

    @PostMapping("/closeAi")
    @ResponseBody
    public AjaxResult closeAi(AiInfo aiInfo){

        if("1".equals(aiInfo.getLoginType())){
            for (Map.Entry<Integer, ConcurrentMap<Integer, IoSession>> integerConcurrentMapEntry : ClientRoleManage.fightAiSessionMap.entrySet()) {
                SpringUtils.getBean(ClientSessionManage.class).removeSession(integerConcurrentMapEntry.getValue());
                integerConcurrentMapEntry.getValue().clear();
            }
        }else  if("0".equals(aiInfo.getLoginType())){
            SpringUtils.getBean(ClientSessionManage.class).removeSession(ClientRoleManage.moveTaskAiSessionMap);
            ClientRoleManage.moveTaskAiSessionMap.clear();
        }
        return AjaxResult.success();
    }
    @PostMapping("/moveTaskAi")
    @ResponseBody
    public AjaxResult moveTaskAi(AiInfo aiInfo){
        ClientRoleManage.moveTaskAi = true;
        SchedulerManager.getInstance().schedule(new Runnable(){
            @Override
            public void run() {
                SpringUtils.getBean(ClientRoleManage.class).moveTaskAI();
            }
        },50);
        return AjaxResult.success();
    }


    @PostMapping("/stopTaskAi")
    @ResponseBody
    public AjaxResult stopTaskAi(AiInfo aiInfo){
        ClientRoleManage.moveTaskAi = false;
        return AjaxResult.success();
    }

    @PostMapping("/moveFightAi")
    @ResponseBody
    public AjaxResult moveFightAi(AiInfo aiInfo){
        ClientRoleManage.moveFightAi = true;
        SchedulerManager.getInstance().schedule(new Runnable(){
            @Override
            public void run() {
                SpringUtils.getBean(ClientRoleManage.class).moveFightAI();
            }
        },50);

        return AjaxResult.success();
    }
    @PostMapping("/stopFightAi")
    @ResponseBody
    public AjaxResult stopFightAi(AiInfo aiInfo){
        ClientRoleManage.moveFightAi = false;
        return AjaxResult.success();
    }

    @PostMapping("/ClearAi")
    @ResponseBody
    public AjaxResult ClearAi(AiInfo aiInfo){
        List<Role> roleList = dao.query(Role.class, Cnd.where("aitype","=","2"));
        for (Role role : roleList) {
            dao.delete(role);
        }

        List<Role> roleList2 = dao.query(Role.class, Cnd.where("aitype","=","1"));
        for (Role role : roleList2) {
            dao.delete(role);
        }
        return AjaxResult.success();
    }


}
